package com.wlw.admin.openeye.ui.adapter

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.mvp.model.bean.HomeBean
import com.bumptech.glide.request.RequestOptions
import com.hazz.kotlinmvp.view.recyclerview.ViewHolder
import com.hazz.kotlinmvp.view.recyclerview.adapter.CommonAdapter
import com.wlw.admin.openeye.glide.GlideRoundTransform


class FollowAdapter(context: Context, data: ArrayList<HomeBean.Issue.Item>) : CommonAdapter<HomeBean.Issue.Item>(context, data, -1) {

    override fun bindData(holder: ViewHolder, data: HomeBean.Issue.Item, position: Int) {
        when {
            mData[position].type == "videoCollectionWithBrief" -> setAuthorInfo(holder, data)
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return when {
            mData[viewType].type == "videoCollectionWithBrief" ->
                ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_follow, parent, false))
            else ->
                throw IllegalAccessException("Api 解析出错了，出现其他类型")
        }
    }

    fun addData(dataList: ArrayList<HomeBean.Issue.Item>) {
        mData.addAll(dataList)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int = mData.size
    private fun setAuthorInfo(holder: ViewHolder, item: HomeBean.Issue.Item) {
        val headerData = item.data?.header
        val myOptions = RequestOptions()
                .centerCrop()
                .transform(GlideRoundTransform(20))

        Glide.with(mContext).load(headerData?.icon)
                .transition(DrawableTransitionOptions().crossFade())
                .apply(myOptions)
                .into(holder.getView(R.id.iv_avatar))

        holder.setText(R.id.tv_title, headerData?.title ?: "")

        val recyclerView = holder.getView<RecyclerView>(R.id.fl_recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(holder.itemView.context, RecyclerView.HORIZONTAL, false)
        recyclerView.adapter = item.data?.itemList?.let { FollowHorizontalAdapter(mContext, it) }
    }
}